package edu.udel.libra.model.repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.springframework.stereotype.Component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import edu.udel.libra.Constants;
import edu.udel.libra.model.League;
import edu.udel.libra.util.Sql;

@Component
public class JdbcLeagueRepository implements LeagueRepository
{
	private static final Logger LOG = Logger.getLogger(JdbcLeagueRepository.class.getName());
	
	private JdbcTemplate jdbcTemplate;
	private FantasyTeamRepository fantasyTeamRepository;
	private PlayerRepository playerRepository;
	private List<League> leagues; 
	
	//private DataSource dataSource;
	
	public void setDataSource(DataSource dataSource) {
		 this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public JdbcLeagueRepository() {
		// TODO: Add database connection
		fantasyTeamRepository = new JdbcFantasyTeamRepository();
		playerRepository = new JdbcPlayerRepository();
		
		// Sample data
		leagues = new ArrayList<League>();
		
		League league1 = new League();
		league1.setId(0);
		league1.setName("Jon's League");
		league1.setFantasyTeams(fantasyTeamRepository.getTeamsForLeague(league1.getId()));
		league1.setPlayers(playerRepository.getPlayersForLeague(league1.getId()));
		
		League league2 = new League();
		league2.setId(1);
		league2.setName("Kyle's League");
		league2.setFantasyTeams(fantasyTeamRepository.getTeamsForLeague(league2.getId()));
		league2.setPlayers(playerRepository.getPlayersForLeague(league2.getId()));
		
		leagues.add(league1);
		leagues.add(league2);
		// endof Sample data
	}
	
	public JdbcLeagueRepository(String alias) {
		//final DataSource ds = Sql.getDataSource(alias);
		/*final DataSource ds = this.dataSource;
        jdbcTemplate = new JdbcTemplate(ds);
		fantasyTeamRepository = new JdbcFantasyTeamRepository(ds);
		playerRepository = new JdbcPlayerRepository(ds);*/
	}
	
	public JdbcLeagueRepository(DataSource dataSource) {
		/*jdbcTemplate = new JdbcTemplate(dataSource);
		fantasyTeamRepository = new JdbcFantasyTeamRepository(dataSource);
		playerRepository = new JdbcPlayerRepository(dataSource);*/
	}
	
	private class LeagueRowMapper implements RowMapper<League> 
	{
        @Override
        public League mapRow(ResultSet rs, int rowNum) throws SQLException {   	
            League league = new League();
            league.setId(rs.getInt("ID"));
            league.setName(rs.getString("NAME"));
            
            league.setPlayers(playerRepository.getPlayersForLeague(league.getId()));
            league.setFantasyTeams(fantasyTeamRepository.getTeamsForLeague(league.getId()));
     
            return league;
        }
    }

	@Override
	public League getLeague(long leagueId) {				
		String query = "select fantasy_league_id as id, league_name as name from " + Constants.DBTABLE_FANTASY_LEAGUE + " where fantasy_league_id = ?";
		LOG.info("getLeague(); query=" + query + "; params=" + leagueId);
		return jdbcTemplate.queryForObject(query, new LeagueRowMapper(), leagueId);
	}

	@Override
	public List<League> getLeagues() {	
		String query = "select fantasy_league_id as id, league_name as name from " + Constants.DBTABLE_FANTASY_LEAGUE + " order by name";
		LOG.info("getLeagues(); query=" + query);
		return jdbcTemplate.query(query, new LeagueRowMapper());
	}
	
	@Override
	public List<League> getLeaguesForUser(long userId) {
		// TODO: Put actual sql
		return getLeagues();
	}

	@Override
	public boolean save(League league) {
		// TODO: Sql INSERT/UPDATE
		
		// If this is a new league - Create new league
		if (league.getId() < 0) {
			league.setId(leagues.size());
			leagues.add(league);
		}
		else {
			// Else - Update league information
			long leagueId = league.getId();
			leagues.set((int) leagueId, league);
		}
		
		return true;
	}


}
